package czh.cocoa.ui.impl;

import czh.cocoa.foundation.NSString;
import czh.cocoa.graphics.CGPoint;
import czh.cocoa.graphics.CGRect;
import czh.cocoa.graphics.CGSize;
import czh.cocoa.ui.UIColor;
import czh.cocoa.ui.UIEvent;
import czh.cocoa.ui.UIFont;
import czh.cocoa.ui.UIImage;
import czh.cocoa.ui.UITouch;

public class UISegmentedControl extends UIControl {

	public final static int UISegmentedControlStylePlain = 0;
	public final static int UISegmentedControlStyleBar = 1;
	
	private int mSegmentedControlStyle;
	
	private int mSelectedSegmentIndex;
	
	private boolean mMomentary;
	
	private UISegment[] mSegments;
	
	public UISegmentedControl() {
		
	}
	
	public UISegmentedControl(CGRect frame) {
		super(frame);
	}
	
	protected void init() {
		super.init();
		mSegments = new UISegment[0];
		mSelectedSegmentIndex = -1;
		mSegmentedControlStyle = UISegmentedControlStylePlain;
	}
	
	public void setSegmentedControlStyle(int style) {
		if (mSegmentedControlStyle != style) {
			mSegmentedControlStyle = style;
			int number = numberOfSegments();
			if (number > 0) {
				setNumberOfSegments(number);
			}
		}
	}
	
	public int numberOfSegments() {
		return mSegments.length;
	}
	
	public void setNumberOfSegments(int number) {
		clearAllSubviews();
		mSegments = new UISegment[number];
		
		int style = mSegmentedControlStyle;
		UIImage divider, segment_left, segment_left2, 
		        segment_middle, segment_middle2, 
		        segment_right, segment_right2;
		
		if (style == UISegmentedControlStyleBar) {
			divider = UIImage.imageNamed(NSTR("cocoa/segment_bar_divider"));
			segment_left = UIImage.imageNamed(NSTR("cocoa/segment_bar_left"));
			segment_left2 = UIImage.imageNamed(NSTR("cocoa/segment_bar_left_highlighted"));
			segment_middle = UIImage.imageNamed(NSTR("cocoa/segment_bar_middle"));
			segment_middle2 = UIImage.imageNamed(NSTR("cocoa/segment_bar_middle_highlighted"));
			segment_right = UIImage.imageNamed(NSTR("cocoa/segment_bar_right"));
			segment_right2 = UIImage.imageNamed(NSTR("cocoa/segment_bar_right_highlighted"));
		} else {
			divider = UIImage.imageNamed(NSTR("cocoa/segment_divider"));
			segment_left = UIImage.imageNamed(NSTR("cocoa/segment_left"));
			segment_left2 = UIImage.imageNamed(NSTR("cocoa/segment_left_highlighted"));
			segment_middle = UIImage.imageNamed(NSTR("cocoa/segment_middle"));
			segment_middle2 = UIImage.imageNamed(NSTR("cocoa/segment_middle_highlighted"));
			segment_right = UIImage.imageNamed(NSTR("cocoa/segment_right"));
			segment_right2 = UIImage.imageNamed(NSTR("cocoa/segment_right_highlighted"));
		}
		
		CGSize divider_size = DIP(divider.size());
		
		CGRect bounds = bounds();
		bounds.size.height = divider_size.height;
		setBounds(bounds);
		
		int seg_width = (int) ((bounds.size.width - divider_size.width * (number - 1)) / number);
		int seg_height = (int) bounds.size.height;
		
		float offx = 0;
		
		for (int i = 0; i < number; i++) {
			UISegment segment = new UISegment();
			segment.setFrame(CGRectMake(offx, 0, seg_width, seg_height));
			if (i == 0 && number > 1) {
				segment.setContentStretch(CGRectMake(0.95f, 0, 0.05f, 1));
				segment.setBackgroundImageForState(segment_left, UIControlStateNormal);
				segment.setBackgroundImageForState(segment_left2, UIControlStateHighlighted);
			} else if (i == number - 1 && number > 1) {
				segment.setContentStretch(CGRectMake(0, 0, 0.05f, 1));
				segment.setBackgroundImageForState(segment_right, UIControlStateNormal);
				segment.setBackgroundImageForState(segment_right2, UIControlStateHighlighted);
			} else {
				//segment.setContentStretch(CGRectMake(0.5f, 0, 0.1f, 1));
				segment.setBackgroundImageForState(segment_middle, UIControlStateNormal);
				segment.setBackgroundImageForState(segment_middle2, UIControlStateHighlighted);
			}
			
			segment.setFont(UIFont.boldSystemFontOfSize(20)); 
			if (style == UISegmentedControlStyleBar) {
				segment.setTitleColorForState(UIColor.whiteColor(), UIControlStateNormal);
			} else {
				segment.setTitleColorForState(UIColor.colorWithWhiteAlpha(0.5f, 1), UIControlStateNormal);
			}
			segment.setTitleColorForState(UIColor.whiteColor(), UIControlStateHighlighted);
			
			addSubview(segment);
			
			offx += seg_width;
			
			if (i != number - 1) {
				UIImageView divider_view = new UIImageView(divider);
				divider_view.setFrame(CGRectMake(offx, 0, divider_size.width, seg_height));
				addSubview(divider_view);
				offx += divider_size.width;
			}
			
			mSegments[i] = segment;
		}
	}
	
	public void setTitleForSegmentAtIndex(NSString title, int index) {
		UISegment segment = mSegments[index];
		segment.setTitleForState(title, UIControlStateNormal);
	}
	
	public void setSelectedSegmentIndex(int selectedIndex) {
		if (mSelectedSegmentIndex != selectedIndex || mMomentary) {
			int index0 = mSelectedSegmentIndex;
			mSelectedSegmentIndex = selectedIndex;
			if (index0 >= 0) {
				mSegments[index0].setHighlighted(false); 
			}
			if(selectedIndex >= 0) {
				mSegments[selectedIndex].setHighlighted(true);
			}
		}
	}
	
	public void setMomentary(boolean momentary) {
		mMomentary = momentary;
	}
	
	public boolean isMomentary() {
		return mMomentary;
	}
	
	private class UISegment extends UIButton {
		
		public boolean beginTrackingWithTouchWithEvent(UITouch touch, UIEvent event) {
			int index = 0;
			for (int i = 0; i < mSegments.length; i++) {
				if (this == mSegments[i]) {
					index = i;
					break;
				}
			}
			setSelectedSegmentIndex(index);
			
			return mMomentary;
		}
		
//		public boolean continueTrackingWithTouchWithEvent(UITouch touch, UIEvent event) {
//			return false;
//		}
//		
//		public void endTrackingWithTouchWithEvent(UITouch touch, UIEvent event) {
//			
//		}
//		
//		public void cancelTrackingWithEvent(UIEvent event) {
//
//		}
		
	}
	
}
